package com.tito.common.framework.controller;

import com.tito.common.api.PageInfo;
import com.tito.common.api.ResultBean;
import com.tito.common.api.query.QueryParams;
import com.tito.common.framework.entity.IDEntity;
import com.tito.common.framework.service.IBaseService;
import com.tito.common.framework.util.IdUtil;
import com.tito.common.framework.util.RequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <pre>
 * 描述：controller的基础类
 * 包含了常用的增删查改的方法，需要定制化可自行覆盖
 * </pre>
 */
public abstract class BaseController<T extends IDEntity> extends GenericController<T>{

    public static final List<String> filterParas = new ArrayList<String>(Arrays.asList("pageNO","pageSize"));

    protected abstract String getModelDesc();

//    @Autowired
    @Resource
    protected IBaseService<T> manager;
    
    /**
     * 分页列表
     */
    @ResponseBody
    @RequestMapping("listJson")
    public PageInfo<T> listJson(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QueryParams queryFilter = RequestUtil.handleRequestParam(request, filterParas);
//        PageBean page = queryFilter.
//        Page<T> p = new Page<T>();
//        p.setSize(page.getPageSize());
//        p.setCurrent(page.getPageNo());
//        p.setSearchCount(true);
//        p
//        p.set
        
//        QueryWrapper<T> ew = convertCondiction(queryFilter);
//
//        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        long pageNO = 1;
        long pageSize = 10;
        if(StringUtils.isNotEmpty(request.getParameter("pageNO")))
        {
            pageNO = Long.valueOf(request.getParameter("pageNO"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("pageSize")))
        {
            pageSize = Long.valueOf(request.getParameter("pageSize"));
        }
        PageInfo<T> pageInfo = new PageInfo<T>();
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(pageNO);

        pageInfo = manager.page(pageInfo, queryFilter);
        /*List<T> l = manager.selectList(ew);
        // 获取总条数，并释放资源
        long total = PageHelper.freeTotal();
        PageResult pageResult = new PageResult(l);
        pageResult.setIsOk(true);
        pageResult.setTotal(total);*/
        return pageInfo;
    }
  
    /**
     * 获取对象
     */
    @ResponseBody
    @RequestMapping("getById")
//    //@CatchErr
    public ResultBean<T> getById(@RequestParam String id) throws Exception {
    	T t = null;
    	if(StringUtils.isNotEmpty(id)) {
    		t = manager.getById(id);
    	}
       return getSuccessResult(t);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("save")
    //@CatchErr
    public ResultBean<String> save(@RequestBody T t) throws Exception {
        String desc;
        if (StringUtils.isEmpty(t.getId())) {
        	t.setId(IdUtil.getSuid());
            desc = "添加%s成功";
            manager.save(t);
        } else {
            manager.updateById(t);
            desc = "更新%s成功";
        }
        return getSuccessResult(String.format(desc, getModelDesc()));
    }

    /**
     * 批量删除
     */
    @ResponseBody
    @RequestMapping("removeByIds")
    //@CatchErr
    public ResultBean<String> removeByIds(@RequestParam String id) throws Exception {
         String[] aryIds = RequestUtil.getStringAryByStr(id);
         manager.removeByIds(Arrays.asList(aryIds));
         return getSuccessResult(String.format("批量删除%s成功", getModelDesc()));
    }

    /**
     * 批量删除
     */
    @ResponseBody
    @RequestMapping("removeById")
    //@CatchErr
    public ResultBean<String> removeById(@RequestParam String id) throws Exception {
        manager.removeById(id);
        return getSuccessResult(String.format("删除%s成功", getModelDesc()));
    }

    @ResponseBody
    @RequestMapping("updateById")
    //@CatchErr
    public ResultBean<String> updateById(@RequestParam T entity) throws Exception {
        manager.updateById(entity);
        return getSuccessResult(String.format("更新%s成功", getModelDesc()));
    }
}